/**
 * 
 */
package com.lanswon.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.common.enums.MessageType;
import com.lanswon.common.util.StringUtil;
import com.lanswon.entity.UserVO;
import com.lanswon.qzsmk.service.commonservice.Personal;
import com.lanswon.service.CoreService;
import com.lanswon.service.UserService;

import net.sf.json.JSONObject;

/**
 * 
 * @ClassName: coreController.java
 * @Description: 核心接口
 *
 * @version: v1.0.0
 * @author: zm
 * @date: 2018年5月29日 上午11:20:05
 *
 */
@Controller
@RequestMapping("front/")
public class CoreController {

	@Autowired
	CoreService coreService;

	@Autowired
	UserService userService;

	/**
	 * 
	 * 1.查询需要挂失卡信息
	 * 
	 * @param startLine
	 * @param numPerPage
	 * @param currentPage
	 * @param request
	 * @return
	 */
	@RequestMapping("queryLossCard")
	@ResponseBody
	public JSONObject queryLossCard(int startLine, int numPerPage, int currentPage, HttpServletRequest request) {

		JSONObject jsonObject = new JSONObject();
		// 登陆验证
		Long userId = (Long) request.getSession().getAttribute("userId");
		if (userId == null) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT012);
			return jsonObject;
		}
		// 参数校验
		// if (StringUtil.isEmpty(citizenCardNo)) {
		// jsonObject.put("code", 1);
		// jsonObject.put("message", MessageType.MT001);
		// return jsonObject;
		// }
		return coreService.queryLossCard(startLine, numPerPage, currentPage, userId);
	}

	/**
	 * 2.提交挂失信息
	 * 
	 * @param citizenCardNo
	 * @param cardPName
	 * @param cardStatus
	 * @param request
	 * @return
	 */
	@RequestMapping("commitLossCardInfo")
	@ResponseBody
	public JSONObject commitLossCardInfo(String citizenCardNo, String cardPName, String cardStatus,
			HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		Long userId = (Long) request.getSession().getAttribute("userId");
		// 登陆验证
		if (userId == null) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT012);
			return jsonObject;
		}
		// 参数校验
		if (StringUtil.isEmpty(citizenCardNo) || StringUtil.isEmpty(cardPName) || StringUtil.isEmpty(cardStatus)) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT001);
			return jsonObject;
		}
		return coreService.commitLossCardInfo(citizenCardNo, cardPName, cardStatus, userId);
	}

	/**
	 * 3.查询需要补卡信息
	 * 
	 * @param startLine
	 * @param numPerPage
	 * @param currentPage
	 * @param request
	 * @return
	 */
	@RequestMapping("querySupplementCard")
	@ResponseBody
	public JSONObject querySupplementCard(int startLine, int numPerPage, int currentPage, HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		Long userId = (Long) request.getSession().getAttribute("userId");
		// 登陆验证
		if (userId == null) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT012);
			return jsonObject;
		}
		return coreService.querySupplementCard(startLine, numPerPage, currentPage, userId);
	}

	/**
	 * 4.提交补卡请求
	 * 
	 * @param oldCitizenCardNo
	 * @param request
	 * @return
	 */
	@RequestMapping("commitSupplementCardInfo")
	@ResponseBody
	public JSONObject commitSupplementCardInfo(String oldCitizenCardNo, HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		Long userId = (Long) request.getSession().getAttribute("userId");

		// 登陆验证
		if (userId == null) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT012);
			return jsonObject;
		}
		// 传参校验
		if (StringUtil.isEmpty(oldCitizenCardNo)) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT001);
			return jsonObject;
		}
		return coreService.commitSupplementCardInfo(oldCitizenCardNo, userId);
	}

	/**
	 * 5.查询网点
	 * 
	 * @param streetId
	 * @param orgProperty
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("queryBranch")
	@ResponseBody
	public Map<String, Object> queryBranch(String streetId, String orgProperty) {
		Map<String, Object> viewMap = new HashMap<>();
		try{
			viewMap = coreService.queryBranch(streetId, orgProperty);
			//viewMap.put("code", 0);
			//viewMap.put("message", "接口调用成功");
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 99);
			viewMap.put("message", MessageType.MT008);
		}
		return viewMap;
	}

	/**
	 * 6.验证能否办理相关卡产品
	 * 
	 * @param request
	 * @param cardProductCode
	 * @param paperType
	 * @param paperNo
	 * @return
	 */
	@RequestMapping("checkIsCardProductbyPaperNo")
	@ResponseBody
	public JSONObject checkIsCardProductbyPaperNo(HttpServletRequest request, String cardProductCode, String paperType,
			String paperNo) {
		JSONObject jsonObject = new JSONObject();
		Long userId = (Long) request.getSession().getAttribute("userId");

		// 登陆验证
		if (userId == null) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT012);
			return jsonObject;
		}
		// 传参校验
		if (StringUtil.isEmpty(paperType) || StringUtil.isEmpty(paperNo)) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT_SR_1);
			return jsonObject;
		}
		if (StringUtil.isEmpty(cardProductCode)) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT_SR_7);
			return jsonObject;
		}
		return coreService.checkIsCardProductbyPaperNo(cardProductCode, paperType, paperNo, userId);
	}

	/**
	 * 7.个人申请提交 singleApplication
	 * 
	 * @param p
	 * @param cardProductCode
	 * @param cardPhysicalType
	 * @param request
	 * @param birthdayStr
	 * @return
	 * @throws ParseException
	 * @throws DatatypeConfigurationException
	 */
	@RequestMapping("singleApplication")
	@ResponseBody
	public JSONObject singleApplication(Personal p, String cardProductCode, String cardPhysicalType,
			HttpServletRequest request, String birthdayStr) throws ParseException, DatatypeConfigurationException {
		JSONObject jsonObject = new JSONObject();
		try {

			if (StringUtil.isEmpty(p.getCustomerName()) || StringUtil.isEmpty(p.getGender())
					|| StringUtil.isEmpty(p.getNationality()) || StringUtil.isEmpty(p.getPaperType())
					|| StringUtil.isEmpty(p.getPaperNo()) || StringUtil.isEmpty(p.getCorpName())
					|| StringUtil.isEmpty(p.getMobile()) || StringUtil.isEmpty(birthdayStr)
					|| StringUtil.isEmpty(cardProductCode)) {
				jsonObject.put("code", 1);
				jsonObject.put("message", MessageType.MT015);
				return jsonObject;
			}
			if (cardProductCode == "15" || cardProductCode == "16") {
				jsonObject.put("code", 2);
				jsonObject.put("message", "银行类卡请前往银行办理");
				return jsonObject;
			}
			// 登陆验证
			if (request.getSession().getAttribute("userId") == null) {
				jsonObject.put("code", 3);
				jsonObject.put("message", MessageType.MT012);
				return jsonObject;
			}
			// birthdayStr="2018-6-1";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			GregorianCalendar gcDate = new GregorianCalendar();
			gcDate.setTime(sdf.parse(birthdayStr));
			XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcDate);
			/*
			 * p.setCustomerName("zj"); p.setGender("男");
			 * p.setNationality("汉族"); p.setPaperType("00");
			 * p.setPaperNo("342401198912108579"); p.setCorpName("蓝远望");
			 * p.setMobile("15907545678");
			 */
			p.setBirthday(xmlGregorianCalendar);
			Long userId = (Long) request.getSession().getAttribute("userId");
			jsonObject = coreService.singleApplication(p, cardProductCode, cardPhysicalType, userId);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return jsonObject;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping("smkApplication")
	@ResponseBody
	public Map<String, Object> smkApplication(HttpServletRequest request, String cardProductCode,
			String cardPhysicalType) {
		Map<String, Object> viewMap = new HashMap<>();
		try {
			if (StringUtil.isEmpty(cardProductCode) || StringUtil.isEmpty(cardPhysicalType)) {
				viewMap.put("code", 1);
				viewMap.put("message", "参数不能为空");
				return viewMap;
			}
			if (cardProductCode == "15" || cardProductCode == "16") {
				viewMap.put("code", 2);
				viewMap.put("message", "银行类卡请前往银行办理");
				return viewMap;
			}
			// 登陆验证
			Long userId = (Long) request.getSession().getAttribute("userId");
			if (userId == null) {
				viewMap.put("code", 3);
				viewMap.put("message", MessageType.MT012);
				return viewMap;
			}
			// 验证信息是否完善
			UserVO user = userService.getUser(userId);
			if (StringUtil.isEmpty(user.getIdNum())) {
				viewMap.put("code", 4);
				viewMap.put("message", "用户信息不完整");
				return viewMap;
			}
			// 参数填充
			Personal p = new Personal();
			// birthdayStr="2018-6-1";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			GregorianCalendar gcDate = new GregorianCalendar();
			gcDate.setTime(sdf.parse(StringUtil.getBirAgeSex(user.getIdNum()).get("birthday").toString()));
			XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcDate);
			
			p.setCustomerName(user.getRealName());
			p.setGender(StringUtil.getBirAgeSex(user.getIdNum()).get("sex").toString());
			// p.setNationality("汉族");
			p.setPaperType("00");
			p.setPaperNo(user.getIdNum());
			// p.setCorpName("蓝远望");
			p.setMobile(user.getMobile());
			p.setBirthday(xmlGregorianCalendar);
			viewMap = coreService.singleApplication(p, cardProductCode, cardPhysicalType, userId);
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 99);
			viewMap.put("message", "申请失败");
			return viewMap;
		}

		return viewMap;
	}

	/**
	 * 8基础信息查询（证件类型）
	 * 
	 * @return
	 */
	@RequestMapping("queryAllCertificates")
	@ResponseBody
	public JSONObject queryAllCertificates(HttpServletRequest request) {
		return coreService.queryAllCertificates();
	}

	/**
	 * 9基础信息查询（卡产品）
	 * 
	 * @return
	 */
	@RequestMapping("queryAllCardProdunctDefinition")
	@ResponseBody
	public JSONObject queryAllCardProdunctDefinition(HttpServletRequest request) {
		return coreService.queryAllCardProdunctDefinition();
	}

	/**
	 * 10基础信息查询（卡物理类型）
	 * 
	 * @return
	 */
	@RequestMapping("queryAllCardPhysicsType")
	@ResponseBody
	public Map<String, Object> queryAllCardPhysicsType() {
		return coreService.queryAllCardPhysicsType();
	}

	/**
	 * 11基础信息查询(个人信息)
	 * 
	 * @return
	 */
	@RequestMapping("queryPersonalByPaperNoAndPaperType")
	@ResponseBody
	public JSONObject queryPersonalByPaperNoAndPaperType(String paperType, String paperNo, HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		// 登陆验证
		if (request.getSession().getAttribute("userId") == null) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT012);
			return jsonObject;
		}
		// paperType = "00"; paperNo = "342401198912108579";
		return coreService.queryPersonalByPaperNoAndPaperType(paperType, paperNo);

	}

	/**
	 * 12基础信息查询(卡相关信息查询)
	 */
	@RequestMapping("queryPersonalCardInfoAndConsumeRecordsByPaperNo")
	@ResponseBody
	public JSONObject queryPersonalCardInfoAndConsumeRecordsByPaperNo(String citizenCardNo, String year,
			String month, String transactionType,String appFlag, HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		if(StringUtil.isEmpty(citizenCardNo)||StringUtil.isEmpty(transactionType)||StringUtil.isEmpty(appFlag)){
			jsonObject.put("code", 1);
			jsonObject.put("message", "卡信息不能为空");
			return jsonObject;
		}
		// 登陆验证
		Long userId = (Long) request.getSession().getAttribute("userId");
		if (userId == null) {
			jsonObject.put("code", 2);
			jsonObject.put("message", MessageType.MT012);
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		if (StringUtil.isEmpty(user.getPapertype()) || StringUtil.isEmpty(user.getPaperno())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "无卡产品");
			return jsonObject;
		}
		return coreService.queryPersonalCardInfoAndConsumeRecordsByPaperNo(citizenCardNo,  year,
				 month,  transactionType, appFlag);
	}

	/**
	 * 13基础信息查询(卡申请进度查询)
	 */
	@RequestMapping("queryApplySchedule")
	@ResponseBody
	public JSONObject queryApplySchedule(String queryCondition, HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		// 登陆验证
		Long userId = (Long) request.getSession().getAttribute("userId");
		if (userId == null) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT012);
			return jsonObject;
		}
		// paperType = "00"; paperNo="342401198912108579"; cardPCode = "80";
		UserVO user = userService.getUser(userId);
		if (StringUtil.isEmpty(user.getPapertype()) || StringUtil.isEmpty(user.getPaperno())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "无卡产品");
			return jsonObject;
		}
		return coreService.queryApplySchedule(user.getPaperno(), user.getPapertype(), queryCondition);
	}

	/**
	 * 14 基础信息查询(通过挂失凭证号查询)
	 */
	@RequestMapping("queryPersonal")
	@ResponseBody
	public JSONObject queryPersonal(String certNo, HttpServletRequest request) {
		JSONObject jsonObject = new JSONObject();
		// 登陆验证
		if (request.getSession().getAttribute("userId") == null) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT012);
			return jsonObject;
		}
		// certNo = "7350532a5ffb11e88c12c85b76d6a059";
		return coreService.queryPersonal(certNo);
	}

	/**
	 * 15 userID卡信息条件查询
	 */
	@RequestMapping("queryCardByUser")
	@ResponseBody
	public JSONObject queryCardByUser(HttpServletRequest request, String citizenCardNo, String year,
			String month, String transactionType,String appFlag) {
		JSONObject jsonObject = new JSONObject();
		// 登陆验证
		if (request.getSession().getAttribute("userId") == null) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT012);
			return jsonObject;
		}
		UserVO user = userService.getUser((Long) request.getSession().getAttribute("userId"));
		if (StringUtil.isEmpty(user.getPapertype()) || StringUtil.isEmpty(user.getPaperno())) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT_CX_1);
			return jsonObject;
		}
		/*
		 * paperType = "00"; paperNo = "320925198809303610"; cardPCode = "80";
		 */

		return coreService.queryPersonalCardInfoAndConsumeRecordsByPaperNo(citizenCardNo,  year,
				 month,  transactionType, appFlag);
	}

	/**
	 * 16基础信息查询(交易查询前,获取用户下所有卡产品)
	 * 
	 * @return
	 */
	@RequestMapping("queryCardProdunctByPersonal")
	@ResponseBody
	public JSONObject queryCardProdunctByPersonal(HttpServletRequest request, String cardPCode) {
		JSONObject jsonObject = new JSONObject();
		// 登陆校验
		Long userId = (Long) request.getSession().getAttribute("userId");
		if (userId == null) {
			jsonObject.put("code", 1);
			jsonObject.put("message", MessageType.MT012);
			return jsonObject;
		}
		UserVO user = userService.getUser(userId);
		/*
		 * String paperType = "00"; String paperNo = "320925198809303619";
		 * cardPCode = "10";
		 */
		if (StringUtil.isEmpty(user.getPapertype()) || StringUtil.isEmpty(user.getPaperno())) {
			jsonObject.put("code", 4);
			jsonObject.put("message", "无卡产品");
			return jsonObject;
		}
		return coreService.queryCardProdunctByPersonal(user.getPapertype(), user.getPaperno(), cardPCode);
	}

	/**
	 * 17.查询网点
	 * 
	 * @return
	 */
	@RequestMapping("queryAllBranchStreet")
	@ResponseBody
	public JSONObject queryAllBranchStreet() {
		return coreService.queryAllBranchStreet();
	}

	/**
	 * 18.查询网点
	 * 
	 * @return
	 */
	@RequestMapping("queryOrgProperty")
	@ResponseBody
	public JSONObject queryOrgProperty() {
		return coreService.queryOrgProperty();
	}

	/**
	 * 19.查询详情
	 * 
	 * @return
	 */
	@RequestMapping("queryBranchByName")
	@ResponseBody
	public JSONObject queryBranchByName(String branchName) {
		return coreService.queryBranchByName(branchName);
	}

}
